package com.hand.wfive.activity.sign

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import com.hand.wfive.R
import com.hand.wfive.adapter.FragmentAdapter
import com.hand.wfive.base.BaseFragment
import com.hand.wfive.entity.FileModel
import com.hand.wfive.entity.ImageVo
import com.hand.wfive.entity.Params
import com.hand.wfive.entity.WordImagesModel
import com.hand.wfive.fragment.sign.SignPreviewItemFragment
import com.hand.wfive.loginAndVip.ApiConfig
import com.hand.wfive.loginAndVip.LoginConfig
import com.hand.wfive.loginAndVip.UserManager
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_sign_preview.*
import rxhttp.wrapper.param.RxHttp

class SignPreviewActivity : BaseSignActivity() {

    companion object {
        fun showIntent(
            context: Context, model: FileModel, data: ArrayList<Pair<Boolean, String>>,
            size: ArrayList<Pair<Int, Int>>, update: Boolean
        ): Intent {
            val intent = Intent(context, SignPreviewActivity::class.java)
            intent.putExtra(Params.update, update)
            intent.putExtra(Params.model, model)
            intent.putExtra(Params.data, data)
            intent.putExtra(Params.size, size)
            return intent
        }
    }

    override fun getContentViewId() = R.layout.activity_sign_preview

    override fun init() {
        super.init()
        loadSaveData()
        if (mSaveData.isEmpty()) {
            finish()
            return
        }
        mUpdate = intent.getBooleanExtra(Params.update, false)

        initTopBar(topBar)
        topBar.setTitle("预览文件")

        val fragments = ArrayList<BaseFragment>()
        mSaveData.forEach {
            fragments.add(SignPreviewItemFragment.load(it.second))
        }
        qvp_sign_preview.adapter = FragmentAdapter(supportFragmentManager, fragments)

    }

    override fun onInitData() {

    }


    private fun loadSaveData() {
        val data = getSerializableExtra(Params.data)
        if (data is ArrayList<*>) data.forEach {
            if (it is Pair<*, *>) {
                mSaveData.add(Pair(it.first as Boolean, it.second.toString()))
            }
        }
    }

    override fun onSave() {
        if (mUpdate) {
            showLoadingTipC()
            loadPdfPageSize()
            if (mFileModel.type == Params.fileTypeWord) {
                if (imageVoList.isEmpty()) {
                    showNormalTip("数据错误，请联系客服")
                } else {
                    var list = ArrayList<ImageVo>()
                    for (i in imageVoList.indices) {
                        if (mSaveData[i].first) {
                            val vo = imageVoList[i]
                            var imageVo = ImageVo(vo.pageIdx, vo.size, vo.slideId, vo.url)
                            imageVo.path = mSaveData[i].second

                            list.add(imageVo)
                        }

                    }
                    saveWord(list, imageVoList)
                }

            } else {
                save()
            }
        } else {
            showNormalTip("未修改，无需保存")
        }
    }

    private fun loadPdfPageSize() {
        mPdfPageSize.clear()
        val size = getSerializableExtra(Params.size)
        if (size is ArrayList<*>) size.forEach {
            if (it is Pair<*, *>) {
                mPdfPageSize.add(Pair(it.first as Int, it.second as Int))
            }
        }
    }
}